﻿<core:ViewBase xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"  x:Class="DXTools.UI.Modules.ODataQueryDesigner.Views.DesignerView"
    xmlns:core="clr-namespace:DXTools.UI.Core.View;assembly=DXTools.UI.Core"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
    xmlns:local="clr-namespace:DXTools.UI.Modules.ODataQueryDesigner;assembly=DXTools.UI.Modules.ODataQueryDesigner"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="800" Margin="5,5,15,5">

    <toolkit:BusyIndicator IsBusy="{Binding IsBusy}">
     
        <FrameworkElement.Resources>
            <local:ObjectConverter x:Key="ConvertObject" />
            <local:EnumBoolConverter x:Key="ConvertEnum" />
            <local:AttributeTextValueConverter x:Key="ConvertTextValue" />

            <!-- Page ScrollViewer Style -->
            <Style x:Key="PageScrollViewerStyle" TargetType="ScrollViewer">
                <Setter Property="BorderBrush" Value="Transparent"/>
                <Setter Property="BorderThickness" Value="0,1,0,1"/>
                <Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
                <Setter Property="HorizontalScrollBarVisibility" Value="Disabled"/>
            </Style>

            <!-- LayoutRoot Grid Style -->
            <Style x:Key="LayoutRootGridStyle" TargetType="Grid">
                <Setter Property="Background" Value="#FFFFFFFF"/>
            </Style>

            <!-- Content Border Style -->
            <Style x:Key="ContentBorderStyle" TargetType="Border">
                <Setter Property="Background">
                    <Setter.Value>
                        <LinearGradientBrush EndPoint="0.5,0.045" StartPoint="0.5,0">
                            <GradientStop Color="#6FCCCCCC"/>
                            <GradientStop Color="#00CCCCCC" Offset="1"/>
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
                <Setter Property="BorderBrush" Value="#FFFFFFFF"/>
                <Setter Property="BorderThickness" Value="0,3,0,0"/>
                <Setter Property="Margin" Value="0,42,0,0"/>
                <Setter Property="VerticalAlignment" Value="Stretch"/>
                <Setter Property="HorizontalAlignment" Value="Stretch"/>
            </Style>


            <!-- Page Items Control Style -->
            <Style TargetType="ItemsControl">
                <Setter Property="Padding" Value="1"/>
                <Setter Property="Background" Value="#FFFFFFFF" />
                <Setter Property="Foreground" Value="#FF000000"/>
                <Setter Property="HorizontalContentAlignment" Value="Left" />
                <Setter Property="VerticalContentAlignment" Value="Top" />
                <Setter Property="IsTabStop" Value="False" />
                <Setter Property="BorderThickness" Value="1" />
                <Setter Property="TabNavigation" Value="Once" />
                <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
                <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
                <Setter Property="BorderBrush">
                    <Setter.Value>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FFA3AEB9" Offset="0"/>
                            <GradientStop Color="#FF8399A9" Offset="0.375"/>
                            <GradientStop Color="#FF718597" Offset="0.375"/>
                            <GradientStop Color="#FF617584" Offset="1"/>
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ItemsControl">
                            <Grid>
                                <Border CornerRadius="2" 
			            BorderBrush="{TemplateBinding BorderBrush}"
			            BorderThickness="{TemplateBinding BorderThickness}">

                                    <ItemsPresenter />

                                </Border>
                                <Border x:Name="ValidationErrorElement" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2" BorderBrush="#FFDB000C" Visibility="Collapsed">
                                    <Grid Width="10" Height="10" HorizontalAlignment="Right" Margin="0,-4,-4,0" VerticalAlignment="Top" Background="Transparent">
                                        <Path Margin="-1,3,0,0" Data="M 1,0 L6,0 A 2,2 90 0 1 8,2 L8,7 z" Fill="#FFDC000C"/>
                                        <Path Margin="-1,3,0,0" Data="M 0,0 L2,0 L 8,6 L8,8" Fill="#ffffff"/>
                                    </Grid>
                                </Border>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

        </FrameworkElement.Resources>

        <Grid x:Name="LayoutRoot">

            <sdk:TabControl  Margin="5,5,0,5" >
                <sdk:TabItem Header="Create Query" >

                    <ScrollViewer  Style="{StaticResource PageScrollViewerStyle}">

                        <StackPanel >
                            <TextBox x:Name="uxODataQuery"  TextWrapping="Wrap" Height="60" AcceptsReturn="True" VerticalScrollBarVisibility="Visible"
                                      Text="{Binding Path=ODataQuery, Mode=TwoWay}" />
                            <Grid >
                                <Grid.ColumnDefinitions >
                                    <ColumnDefinition Width="150" />
                                    <ColumnDefinition Width="150" />
                                    <ColumnDefinition Width="200" />
                                    <ColumnDefinition Width="200" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Button Content="Generate" Grid.Column="0" HorizontalAlignment="Left"  VerticalAlignment="Top" Margin="0,0,0,0" Width="100" Height="23" Command="{Binding GenerateCommand}" />
                                <Button Content="Execute"  Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,0,0,0" Width="100" Height="23" Command="{Binding ExecuteCommand}" />
                                <TextBlock Grid.Column="2" HorizontalAlignment="Left" VerticalAlignment="Center" Width="Auto" Margin="5,0,5,0" FontSize="12" FontWeight="Bold"  Text="{Binding Path=ResultXml}" />
                                <Button Content="Show In Browser"  Grid.Column="3" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5,0,0,0" Width="100" Height="23"  Command="{Binding OpenBrowserCommand}" />
                                <TextBlock Grid.Column="4" HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="Auto" Margin="0,0,5,0" FontSize="12" FontWeight="Bold"  Text="{Binding Path=QueryLength}" />
                            </Grid>
                            <Grid>
                                <Grid.ColumnDefinitions >
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="50" />
                                    <ColumnDefinition />
                                </Grid.ColumnDefinitions>

                                <Grid Grid.Column="0" HorizontalAlignment="Stretch" MinHeight="40" >
                                    <Grid.ColumnDefinitions >
                                        <ColumnDefinition Width="120" />
                                        <ColumnDefinition  />
                                    </Grid.ColumnDefinitions>

                                    <TextBlock Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Left" FontWeight="Bold" Text="Select Entity Set" />
                                    <ComboBox Grid.Column="1" HorizontalAlignment="Stretch"  VerticalAlignment="Center" ItemsSource="{Binding Path=EntityList,  Mode=OneWay}" DisplayMemberPath="SchemaNameSet" SelectedValuePath="LogicalName" SelectedItem="{Binding Path=SelectedEntity, Mode=TwoWay}" />
                                </Grid>
                            </Grid>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="40" />
                                    <RowDefinition Height="20" />
                                    <RowDefinition Height="200" />
                                </Grid.RowDefinitions>
                                <TextBlock Grid.Row="0" Text="Filter Criteria" FontWeight="Bold" VerticalAlignment="Center" />
                                <StackPanel Grid.Row="1" Orientation="Horizontal">
                                    <Button Content="Group AND" Margin="5,0,5,0"  Command="{Binding AddGroupANDCommand}" />
                                    <Button Content="Group OR" Margin="5,0,5,0" Command="{Binding AddGroupORCommand}" />
                                </StackPanel>
                                <ItemsControl Grid.Row="2" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch"   ItemsSource="{Binding Path=FiltersList}"  >
                                    <ItemsControl.ItemTemplate>
                                        <DataTemplate >
                                            <Grid  HorizontalAlignment="Stretch" Height="30" Background="{Binding Path=RowColour}"  >
                                                <!-- The attribute picklist is shown first -->
                                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Center"  Visibility="{Binding Path=AttributeListVisibilityLevel}" >
                                                    <ComboBox Width="450" Margin="5,0,1,0" MaxDropDownHeight="400" ItemsSource="{Binding Path=Attributes}" DisplayMemberPath="UserLocalizedLabel" SelectedItem="{Binding Path=SelectedAttribute, Mode=TwoWay}" />
                                                </StackPanel>
                                                <!-- Once the attribute is selected then this full row of options is shown -->
                                                <StackPanel  Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Center" Visibility="{Binding Path=SelectFilterVisibilityLevel}" >
                                                    <Grid VerticalAlignment="Center">
                                                        <ComboBox Width="35" Margin="5,0,1,0" ItemsSource="{Binding Path=FilterActions}" SelectedItem="{Binding Path=FilterAction, Mode=TwoWay}" Visibility="{Binding Path=FilterActionsVisibilityLevel}"   />
                                                        <TextBlock Width="35" Margin="5,0,1,0" Text="{ Binding Path=GroupLogicalOperator }" Visibility="{Binding Path=LogicalOperatorVisibilityLevel}" />
                                                    </Grid>
                                                    <ComboBox Width="350" Margin="1,0,1,0" MaxDropDownHeight="400" ItemsSource="{Binding Path=Attributes}" DisplayMemberPath="UserLocalizedLabel" SelectedItem="{Binding Path=SelectedAttribute, Mode=TwoWay}" />
                                                    <ComboBox Width="200" Margin="1,0,1,0" ItemsSource="{Binding Path=FilterOperators}" SelectedItem="{Binding Path=FilterOperator, Mode=TwoWay}"  />

                                                    <Grid VerticalAlignment="Center">
                                                        <TextBlock Width="300" />
                                                        <TextBox Width="300" Margin="1,0,1,0" Text="{Binding Path=Value, Mode=TwoWay}" Visibility="{Binding Path=TextValueVisibilityLevel}" />
                                                        <ComboBox Width="300" Margin="1,0,1,0" ItemsSource="{Binding Path=AttributeValues}" DisplayMemberPath="Name" SelectedValuePath="Value" SelectedItem="{Binding Path=Value, Mode=TwoWay, Converter={StaticResource ConvertTextValue}}"  Visibility="{Binding Path=OptionSetValueVisibilityLevel}" />

                                                    </Grid>
                                                    <ComboBox Width="60" Margin="1,0,1,0" ItemsSource="{Binding Path=LogicalOperators}" SelectedItem="{Binding Path=LogicalOperator, Mode=TwoWay}" />
                                                </StackPanel>
                                            </Grid>
                                        </DataTemplate>
                                    </ItemsControl.ItemTemplate>
                                </ItemsControl>

                            </Grid>
                            <Grid>

                                <Grid.ColumnDefinitions >
                                    <ColumnDefinition />
                                    <ColumnDefinition Width="50" />
                                    <ColumnDefinition />
                                </Grid.ColumnDefinitions>

                                <StackPanel  Grid.Column="0">


                                    <sdk:TabControl>
                                        <sdk:TabItem Header="Entity Attributes" >
                                            <Grid>

                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="40" />
                                                    <RowDefinition Height="*" />
                                                </Grid.RowDefinitions>

                                                <ListBox Grid.Row="1" Height="250" HorizontalAlignment="Stretch" ItemsSource="{Binding AttributeList}">
                                                    <ListBox.ItemTemplate>
                                                        <DataTemplate>
                                                            <StackPanel Orientation="Horizontal" Width="180" VerticalAlignment="Top" HorizontalAlignment="Left">
                                                                <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay}" Width="16"  Margin="2,2,0,0" ClickMode="Press"></CheckBox>
                                                                <TextBlock Text="{Binding Path=UserLocalizedLabel}" FontSize="12" FontFamily="Arial" Margin="5,0,0,0"></TextBlock>
                                                            </StackPanel>
                                                        </DataTemplate>
                                                    </ListBox.ItemTemplate>
                                                </ListBox>
                                            </Grid>
                                        </sdk:TabItem>
                                        <sdk:TabItem Header="One To Many" >
                                            <Grid>

                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="40" />
                                                    <RowDefinition Height="*" />
                                                </Grid.RowDefinitions>


                                                <ComboBox Name="uxOneToMany" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="2"   ItemsSource="{Binding Path=OneToMany_RelationshipList,  Mode=OneWay}"  DisplayMemberPath="SchemaName"  SelectedItem="{Binding Path=SelectedOneToManyRelationship, Mode=TwoWay}" />

                                                <ListBox  Grid.Row="1" Height="250" HorizontalAlignment="Stretch" ItemsSource="{Binding OneToMany_RelationshipAttributeList}"    >
                                                    <ListBox.ItemTemplate>
                                                        <DataTemplate>
                                                            <StackPanel Orientation="Horizontal" Width="180" VerticalAlignment="Top" HorizontalAlignment="Left">
                                                                <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay}" Width="14" Height="14" Margin="2,2,0,0" ClickMode="Press"></CheckBox>
                                                                <TextBlock Text="{Binding Path=UserLocalizedLabel}" FontSize="12" FontFamily="Arial" Margin="5,0,0,0"></TextBlock>
                                                            </StackPanel>
                                                        </DataTemplate>
                                                    </ListBox.ItemTemplate>
                                                </ListBox>

                                            </Grid>

                                        </sdk:TabItem>

                                        <sdk:TabItem Header="Many To One" >
                                            <Grid>

                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="40" />
                                                    <RowDefinition Height="*" />
                                                </Grid.RowDefinitions>
                                                <ComboBox Name="uxManyToOne" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="2"   ItemsSource="{Binding Path=ManyToOne_RelationshipList,  Mode=OneWay}"  DisplayMemberPath="SchemaName"  SelectedItem="{Binding Path=SelectedManyToOneRelationship, Mode=TwoWay}" />

                                                <ListBox Grid.Row="1" Height="250" HorizontalAlignment="Stretch" ItemsSource="{Binding Path=ManyToOne_RelationshipAttributeList}"    >
                                                    <ListBox.ItemTemplate>
                                                        <DataTemplate>
                                                            <StackPanel Orientation="Horizontal" Width="180" VerticalAlignment="Top" HorizontalAlignment="Left">
                                                                <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay}" Width="14" Height="14" Margin="2,2,0,0" ClickMode="Press"></CheckBox>
                                                                <TextBlock Text="{Binding Path=UserLocalizedLabel}" FontSize="12" FontFamily="Arial" Margin="5,0,0,0"></TextBlock>
                                                            </StackPanel>
                                                        </DataTemplate>
                                                    </ListBox.ItemTemplate>
                                                </ListBox>
                                            </Grid>

                                        </sdk:TabItem>

                                        <sdk:TabItem Header="Many To Many" >
                                            <Grid>

                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="40" />
                                                    <RowDefinition Height="*" />
                                                </Grid.RowDefinitions>
                                                <ComboBox Name="uxManyToMany" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="2"   ItemsSource="{Binding Path=ManyToMany_RelationshipList,  Mode=OneWay}"  DisplayMemberPath="SchemaName"  SelectedItem="{Binding Path=SelectedManyToManyRelationship, Mode=TwoWay}" />

                                                <ListBox  Grid.Row="1" Height="250" HorizontalAlignment="Stretch" ItemsSource="{Binding Path=ManyToMany_RelationshipAttributeList}"    >
                                                    <ListBox.ItemTemplate>
                                                        <DataTemplate>
                                                            <StackPanel Orientation="Horizontal" Width="180" VerticalAlignment="Top" HorizontalAlignment="Left">
                                                                <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay}" Width="14" Height="14" Margin="2,2,0,0" ClickMode="Press"></CheckBox>
                                                                <TextBlock Text="{Binding Path=UserLocalizedLabel}" FontSize="12" FontFamily="Arial" Margin="5,0,0,0"></TextBlock>
                                                            </StackPanel>
                                                        </DataTemplate>
                                                    </ListBox.ItemTemplate>
                                                </ListBox>
                                            </Grid>

                                        </sdk:TabItem>
                                    </sdk:TabControl>

                                </StackPanel>
                                <StackPanel  Grid.Column="2">
                                    <TextBlock Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Left" FontWeight="Bold" Height="20" Text="Order By Criteria" />

                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="100"/>
                                            <ColumnDefinition />
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Grid.Column="0" x:Name="Sort1Label" Text="Sort By" Margin="5,2,0,0" VerticalAlignment="Top" TextWrapping="Wrap" TextAlignment="Left"/>
                                        <StackPanel Grid.Column="1">
                                            <ComboBox HorizontalAlignment="Stretch" ItemsSource="{Binding Path=SortByAttributesList}" DisplayMemberPath="UserLocalizedLabel" SelectedItem="{Binding Path=SelectedSortBy1Attribute, Mode=TwoWay}" />
                                            <RadioButton  GroupName="Sort1Direction" Content="Ascending Order" IsChecked="{Binding Path=SortBy1Direction, Mode=TwoWay, Converter={StaticResource ConvertEnum}, ConverterParameter=asc}" />
                                            <RadioButton  GroupName="Sort1Direction" Content="Descending Order" IsChecked="{Binding Path=SortBy1Direction, Mode=TwoWay, Converter={StaticResource ConvertEnum}, ConverterParameter=desc}" />

                                        </StackPanel>
                                    </Grid>
                                    <Grid Margin="0,10,0,0">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="100"/>
                                            <ColumnDefinition />
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Grid.Column="0" x:Name="Sort2Label" Text="Then By" Margin="5,2,0,0" VerticalAlignment="Top" TextWrapping="Wrap" TextAlignment="Left"/>
                                        <StackPanel Grid.Column="1">
                                            <ComboBox HorizontalAlignment="Stretch" ItemsSource="{Binding Path=SortByAttributesList}" DisplayMemberPath="UserLocalizedLabel" SelectedItem="{Binding Path=SelectedSortBy2Attribute, Mode=TwoWay}" />
                                            <RadioButton  GroupName="Sort2Direction" Content="Ascending Order" IsChecked="{Binding Path=SortBy2Direction, Mode=TwoWay, Converter={StaticResource ConvertEnum}, ConverterParameter=asc}" />
                                            <RadioButton  GroupName="Sort2Direction" Content="Descending Order" IsChecked="{Binding Path=SortBy2Direction, Mode=TwoWay, Converter={StaticResource ConvertEnum}, ConverterParameter=desc}" />

                                        </StackPanel>
                                    </Grid>

                                </StackPanel >

                            </Grid>


                        </StackPanel >
                    </ScrollViewer>
                </sdk:TabItem>
                <!-- Create Query -->


                <sdk:TabItem Header="Results (ATOM)" >
                    <TextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="5,5,5,5" Text="{Binding Path=ATOMResultXml}"  
                        HorizontalScrollBarVisibility="Visible"
                        VerticalScrollBarVisibility="Visible"
                        TextWrapping="NoWrap" />
                </sdk:TabItem>
                <sdk:TabItem Header="Results (JSON)" >
                    <TextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="5,5,5,5" Text="{Binding Path=JSONResultXml}"  
                        HorizontalScrollBarVisibility="Visible"
                        VerticalScrollBarVisibility="Visible"
                        TextWrapping="NoWrap" />
                </sdk:TabItem>
                <sdk:TabItem Header="Code Generator" >
                    <TextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="5,5,5,5" Text="{Binding Path=CodeGeneratorScript}"  
                        HorizontalScrollBarVisibility="Visible"
                        VerticalScrollBarVisibility="Visible"
                        TextWrapping="NoWrap" />
                </sdk:TabItem>



            </sdk:TabControl>





        </Grid>

    </toolkit:BusyIndicator>


    
      


</core:ViewBase>
